<template>
  <z-modal
      v-bind="modalOption"
      @ok="handleOk"
      @cancel="handleCancel"
      cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model layout="horizontal" labelAlign="right" ref="ruleForm" :label-col="labelCol" :wrapper-col="wrapperCol" :model="form" :rules="validatorRules">
        <a-form-model-item label="成员编号" prop="userId">
          <z-search-select-tag placement="请选择成员" v-model="form.userId" url="/user/list" optionValue="username" optionText="nickname" @change="searchChange"
                               :disabled="disableSubmit" />
        </a-form-model-item>
        <a-form-model-item label="成员姓名" prop="userName">
          <a-input :disabled="true" v-model.trim="form.userName"/>
        </a-form-model-item>
        <a-form-model-item label="职责" prop="userType">
          <a-radio-group :disabled="disableSubmit" v-model="form.userType">
            <a-radio value="1">设计师</a-radio>
            <a-radio value="2">校对</a-radio>
            <a-radio value="3">后工</a-radio>
          </a-radio-group>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </z-modal>
</template>

<script>
import ModalFormMixin from '@/mixins/ModalFormMixin'
import { postAction, putAction } from '@/services/manage'
import ZSearchSelectTag from '@/components/business/ZSearchSelectTag'
export default {
  name: 'GroupUserModal',
  components: { ZSearchSelectTag },
  mixins: [ModalFormMixin],
  props: ['code'],
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      form: {},
      validatorRules: {
        userName: [{ required: true, message: '请选择成员' }],
        userType: [{ required: true, message: '请选择成员职责' }]
      },
      disableSubmit: false
    }
  },
  methods: {
    searchChange(val, text, ele) {
      this.form = ele
      this.form.userId = val
      this.form.userName = text
    },
    // 确定
    handleOk() {
      // 触发表单验证
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.confirmLoading = true
          const formData = Object.assign(this.model, this.form)
          formData.groupId = this.code
          if (formData.id) {
            putAction('/doc/groupuser/v1/edit', formData).then(res => {
              if (res.success) {
                this.$message.success('操作成功')
                this.$emit('ok')
                this.close()
              }
            }).finally(() => {
              this.confirmLoading = false
            })
          } else {
            postAction('/doc/groupuser/v1/add', formData).then(res => {
              if (res.success) {
                this.$message.success('操作成功')
                this.$emit('ok')
                this.close()
              }
            }).finally(() => {
              this.confirmLoading = false
            })
          }
        }
      })
    },
    // 关闭
    handleCancel() {
      this.close()
    },
    close() {
      this.$emit('close')
      this.$refs.ruleForm.resetFields()
      this.visible = false
    }
  }
}
</script>
